<template>
	<div class="system-edit-dept-container">
		<el-dialog :title="(formData.id!==0?'修改':'添加')+'会员'" v-model="isShowDialog" width="769px" @close="closeDialog">
			<el-form ref="formRef" :model="formData" :rules="rules" size="default" label-width="90px">
				<el-row :gutter="35">
			
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="名称" prop="name">
							<el-input v-model="formData.name" placeholder="请输入名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="联系方式" prop="contact">
							<el-input v-model="formData.contact" placeholder="请输入联系方式" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="状态">
							<el-switch v-model="formData.status" :active-value="1" :inactive-value="0" inline-prompt active-text="启用" inactive-text="停用"></el-switch>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="备注">
							<el-input v-model="formData.remark" type="textarea" placeholder="请输入备注" maxlength="150"></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="入会员工">
							
						  <el-select
						  v-model="opvalue"
						  filterable
						  remote
						  clearable
						  reserve-keyword
						  placeholder="请输入姓名或联系方式"
						  :remote-method="employeeSearch"
						  :loading="loading"
						>
						  <el-option
							v-for="item in options"
							:key="item.id"
							:label="item.name"
							:value="item.id"
						  />
						</el-select>
						</el-form-item>
					</el-col>
					
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{formData.id!==0?'修 改':'添 加'}}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import {reactive, toRefs, defineComponent, getCurrentInstance,ref,unref} from 'vue';
import {edit} from "/@/api/zmm/member";
import {getOpQueryList} from "/@/api/zmm/employee";

import {ElMessage} from "element-plus";

// 定义接口来定义对象的类型
interface TableDataRow {
	name: string;
	contact: string;
	remark: string;
	status: number;
	children?: TableDataRow[];
}
interface FormState {
  id:number;
	name: string;
	contact: string;
	remark: string;
	status: number;
	join_employee_id:number;
	join_employee: {id:number,name:string};
}
interface PageSate {
	isShowDialog: boolean;
	formData: FormState;
	list: Array<TableDataRow>;
  rules: object;
}


export default defineComponent({
	name: 'zmmEditShop',
	setup(prop,{emit}) {
    const {proxy} = getCurrentInstance() as any;
	const loading = ref(false);
	const options = ref<FormState[]>([]);
	const opvalue = ref<any>([]);
	let re_row:FormState

    const formRef = ref<HTMLElement | null>(null);
		const state = reactive<PageSate>({
			isShowDialog: false,
			formData: {
        		id:0,
				name: '', // 部门名称
				contact:'',
				remark:'',
				status: 1,
				join_employee:{id:0,name:""},
				join_employee_id:0,

			},
			list: [], // 数据
      rules: {
        name:[
          {required: true, message: "部门名称不能为空", trigger: "blur"},
	  ],contact:[
		{required: true, message: "联系方式不能为空", trigger: "blur"},
	  ]
      }
		});
		// 打开弹窗
		const openDialog = (row?: FormState|number) => {
      resetForm()
    //   getList().then((res:any)=>{
	// 	const list = { id: 0, name: '主店铺', children: [] };
	// 	list.children = proxy.handleTree(res.data.zmm_shop_list??[], "id","up_id");
    //       state.list=new Array(list) as any;
    //   });
		
      if(row && typeof row === "object"){
        state.formData = row;
		re_row = {...row};
		options.value = [row.join_employee]
		opvalue.value = row.join_employee.id;
		
		console.log(opvalue)
		console.log(row)
      }
			state.isShowDialog = true;
		};
		// 关闭弹窗
		const closeDialog = () => {
			state.isShowDialog = false;
			console.log(re_row)
			state.formData = re_row;
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 新增
		const onSubmit = () => {
      const formWrap = unref(formRef) as any;
      if (!formWrap) return;
      formWrap.validate((valid: boolean) => {
        if (valid) {
        //   if(state.ruleForm.deptId===0){
        //     //添加
        //     addDept(state.ruleForm).then(()=>{
        //       ElMessage.success('角色添加成功');
        //       closeDialog(); // 关闭弹窗
        //       emit('deptList')
        //     });
        //   }else{
			state.formData.join_employee_id = parseInt(opvalue.value?opvalue.value:0)
            //修改
            edit(state.formData).then(()=>{
              ElMessage.success('修改成功');
              closeDialog(); // 关闭弹窗
              emit('list')
            });
        //   }
        }
      });
		};
		const employeeSearch = (query: string)=>{
			if(query){
				loading.value = true
				getOpQueryList({
					op_query:query,
				}).then((res)=>{
					loading.value = false
					options.value = res.data.list
				})
			}
		};
    const resetForm = ()=>{
      state.formData = {
        id:0,
        name: '', 
				contact:'',
				remark:'',
				status: 1,
				join_employee:{id:0,name:""},
				join_employee_id:0,

      }
    };
		return {
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			employeeSearch,
      formRef,
	  loading,
	  options,
	  opvalue,
			...toRefs(state),
		};
	},
});
</script>
